Page({
  data:{
    status:null,
    address_id:null,
    name:"",
    tel:"",
    prodive:"",
    city:"",
    prodiveID:"",
    cityID:"",
    area:"",
    is_default:0,
    nowIndex:0,
    nowIndex2:0,
    pro:[],
    ci:[],
    proA:"",
    ciA:""
  },
  onLoad:function(e){
    this.setData({
      status:e.add_addressStatus,
      address_id:e.addressId
    })
    var self=this;
    wx.request({
      url:"https://wx.ichelaba.com/htapi/index.php/user/address/add",
      data:{
        user_id:wx.getStorageSync("user_id")
      },
      method:"GET",
      success:function(res){
        var province=[];
        for(var i=0;i<res.data.province_list.length;i++){
          province.push(res.data.province_list[i].district_name);
        }
        self.setData({
          pro:province,
          proA:res.data.province_list
        })
        if(e.add_addressStatus==1){
          wx.request({
            url:"https://wx.ichelaba.com/htapi/index.php/user/address/getCity",
            data:{
              user_id:wx.getStorageSync("user_id"),
              pid:self.data.proA[0].district_id
            },
            method:"GET",
            success:function(res){
              var ci=[];
              for(var i=0;i<res.data.list.length;i++){
                ci.push(res.data.list[i]["district_name"]);
              }
              self.setData({
                ciA:res.data.list,
                ci:ci
              })
              wx.hideLoading();
            }
          })
        }else if(e.add_addressStatus==2){
          wx.showLoading();
          wx.request({
            url:"https://wx.ichelaba.com/htapi/index.php/user/address/index",
            data:{
              user_id:wx.getStorageSync("user_id")
            },
            method:"GET",
            success:function(res){
                  var thisIndex=0;
                  var index=0;
                  var index2=0;
                  var prID=0;
                  var cID=0;
                  var a=1;
                  var upRes=res;
                  for(var i=0;i<res.data.address_list.length;i++){
                    if(res.data.address_list[i].address_id==e.addressId){
                      thisIndex=i;
                      cID=res.data.address_list[i].cid
                      prID=res.data.address_list[i].pid;
                    }
                  }
                  for(var i=0;i<self.data.proA.length;i++){
                    if(self.data.proA[i].district_id==res.data.address_list[thisIndex].pid){
                      index=i;
                    }
                  }
                  wx.request({
                    url:"https://wx.ichelaba.com/htapi/index.php/user/address/getCity",
                    data:{
                      user_id:wx.getStorageSync("user_id"),
                      pid:prID
                    },
                    method:"GET",
                    success:function(res){
                      var ci=[];
                      for(var i=0;i<res.data.list.length;i++){
                        ci.push(res.data.list[i]["district_name"]);
                      }
                      self.setData({
                        ciA:res.data.list,
                        ci:ci
                      })
                      self.data.ciA=res.data.list;
                      for(var i=0;i<res.data.list.length;i++){
                        if(res.data.list[i].district_id==upRes.data.address_list[thisIndex].cid){
                          index2=i;
                        }
                      }
                      self.setData({
                        "nowIndex":index,
                        "nowIndex2":index2
                      })
                      wx.hideLoading();
                    }
                  })
                  self.setData({
                    "area":res.data.address_list[thisIndex].address,
                    "name":res.data.address_list[thisIndex].contact,
                    "tel":res.data.address_list[thisIndex].mobile,
                    "is_default":res.data.address_list[thisIndex].is_default
                  })
                }
          })
        }
        
      }
    })
    
  },
  add_FN:function(){
  },
  name_FN:function(e){
    this.data.name=e.detail.value;
  },
  tel_FN:function(e){
    this.data.tel=e.detail.value;
  },
  area_FN:function(e){
    this.data.area=e.detail.value;
  },
  bindPickerChange:function(e){
    this.setData({
      nowIndex:e.detail.value[0]
    })
    if(e.detail.value[1]==null){
      this.setData({
        nowIndex2:0
      })
    }else{
      this.setData({
        nowIndex2:e.detail.value[1]
      })
    }
  },
  bindPickerChangeA:function(e){
    var proID=null;
    var self=this;
    if(e.detail.column==0){
      for(var i=0;i<this.data.proA.length;i++){
        if(this.data.proA[i]["district_name"]==this.data.pro[e.detail.value]){
          proID=this.data.proA[i]["district_id"];
        }
      }
      wx.request({
        url:"https://wx.ichelaba.com/htapi/index.php/user/address/getCity",
        data:{
          user_id:wx.getStorageSync("user_id"),
          pid:proID
        },
        method:"GET",
        success:function(res){
          var ci=[];
          for(var i=0;i<res.data.list.length;i++){
            ci.push(res.data.list[i]["district_name"]);
          }
          self.setData({
            ciA:res.data.list,
            ci:ci
          })
        }

      })
    }
  },
  switchChange:function(e){
    if(e.detail.value){
      this.data.is_default=1;
    }else{
      this.data.is_default=0;
    }
  },
  sure_FN:function(){
    var self=this;
    if(this.data.name==""){
      wx.showModal({
        "title":"提示",
        "content":"请输入姓名",
        "showCancel":false
      })
    }else if(this.data.tel==""){
      wx.showModal({
        "title":"提示",
        "content":"请输入手机号",
        "showCancel":false
      })
    }else if(this.data.area==""){
      wx.showModal({
        "title":"提示",
        "content":"请输入详细地址",
        "showCancel":false
      })
    }
    wx.showLoading();
    if(this.data.status==1){
      wx.request({
        url:"https://wx.ichelaba.com/htapi/index.php/user/address/save",
        data:{
          user_id:wx.getStorageSync("user_id"),
          contact:self.data.name,
          mobile:self.data.tel,
          pname:self.data.proA[self.data.nowIndex]["district_name"],
          pid:self.data.proA[self.data.nowIndex]["district_id"],
          cname:self.data.ciA[self.data.nowIndex2]["district_name"],
          cid:self.data.ciA[self.data.nowIndex2]["district_id"],
          address:self.data.area,
          is_default:self.data.is_default,
        },
        method:"GET",
        success:function(res){
          wx.hideLoading();
          if(res.data.result){
            wx.showToast({
              "title":"添加成功",
              "mask":true,
              complete:function(){
                wx.reLaunch({
                  url:"/pages/address/address"
                })
              }
            })
          }else{
            wx.showModal({
              "title":"提示",
              "content":"请输入正确手机号",
              "showCancal":false
            })
          }
        }
      })
    }else if(this.data.status==2){
      wx.request({
        url:"https://wx.ichelaba.com/htapi/index.php/user/address/save",
        data:{
          address_id:self.data.address_id,
          user_id:wx.getStorageSync("user_id"),
          contact:self.data.name,
          mobile:self.data.tel,
          pname:self.data.proA[self.data.nowIndex]["district_name"],
          pid:self.data.proA[self.data.nowIndex]["district_id"],
          cname:self.data.ciA[self.data.nowIndex2]["district_name"],
          cid:self.data.ciA[self.data.nowIndex2]["district_id"],
          address:self.data.area,
          is_default:self.data.is_default,
        },
        method:"GET",
        success:function(res){
          wx.hideLoading();
          if(res.data.result){
            wx.showToast({
              "title":"修改成功",
              "mask":true,
              complete:function(){
                wx.reLaunch({
                  url:"/pages/address/address"
                })
              }
            })
          }else{
            wx.showModal({
              "title":"提示",
              "content":res.data.message,
              "showCancal":false
            })
          }
        }
      })
    }
    
  }
})